package wiki.hitime.stl.daq.service.factory.impl.msg.wechat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import wiki.hitime.stl.daq.bean.bo.MsgRuleConfigFieldBO;
import wiki.hitime.stl.daq.enums.MsgTypeEnum;
import wiki.hitime.stl.daq.feign.DingTalkFeignClient;
import wiki.hitime.stl.daq.feign.WechatFeignClient;
import wiki.hitime.stl.daq.service.factory.DataMsgFactory;

import java.util.*;

/**
 * @author zhangdaoping
 * @create 2020-01-08 11:46
 */
@Service
@Slf4j
public class WechatMsgImpl implements DataMsgFactory {

    private WechatFeignClient wechatFeignClient;

    public WechatMsgImpl(WechatFeignClient wechatFeignClient) {
        this.wechatFeignClient = wechatFeignClient;
    }

    /**
     * 获取支持的消息渠道类型
     *
     * @return 消息渠道类型枚举
     */
    @Override
    public MsgTypeEnum getSupportMsgType() {
        return MsgTypeEnum.WECHAT;
    }

    /**
     * 获取必须的基础配置键
     *
     * @return
     */
    @Override
    public List<MsgRuleConfigFieldBO> listRequiredBaseConfigInfo() {
        return Collections.singletonList(MsgRuleConfigFieldBO.builder()
                .configName("环境")
                .configKey("profile")
                .description("环境标识，比如：local，生产为：prod")
                .build());
    }

    /**
     * 获取必须的规则详细配置键
     *
     * @return
     */
    @Override
    public List<MsgRuleConfigFieldBO> listRequiredRuleDetailConfigInfo() {
        List<MsgRuleConfigFieldBO> msgRuleConfigFieldBOList = new ArrayList<>();
        msgRuleConfigFieldBOList.add(MsgRuleConfigFieldBO.builder()
                .configName("webhook")
                .configKey("webhook")
                .description("调用键值")
                .build());
        msgRuleConfigFieldBOList.add(MsgRuleConfigFieldBO.builder()
                .configName("@选项")
                .configKey("mentioned_mobile_list")
                .description("消息发出@选项，@某个成员(逗号分隔)，@all表示提醒所有人")
                .build());
        return msgRuleConfigFieldBOList;
    }


    /**
     * 通知消息
     * 实现类增加取值前建议维护 listRequiredBaseConfigInfo 和 listRequiredRuleDetailConfigInfo
     *
     * @param receiverStr   接收人以","隔开
     * @param msgContent    消息内容
     * @param baseConfigMap 基础配置信息
     * @param ruleConfigMap 规则对应配置信息
     * @return
     */
    @Override
    public Boolean notifyMsg(String receiverStr, String msgContent, Map<String, String> baseConfigMap, Map<String, String> ruleConfigMap) {
        try {
            // 实现类增加取值前建议维护 listRequiredBaseConfigInfo 和 listRequiredRuleDetailConfigInfo
            String webhook = ruleConfigMap.get("webhook");
            String profile = baseConfigMap.get("profile");
            String mobileListStr = ruleConfigMap.get("mentioned_mobile_list");

            JSONObject bodyObj = new JSONObject();
            JSONObject textObj = new JSONObject();
            String content = "";
            if (!"prod".equals(profile)) {
                content = "[" + profile + "]\n";
            }
            content += msgContent;
            textObj.put("content", content);

            JSONArray atMobiles = new JSONArray();
            if (!StringUtils.isEmpty(mobileListStr)) {
                List<String> remindUsers = Arrays.asList(mobileListStr.split(","));
                if (!CollectionUtils.isEmpty(remindUsers)) {
                    atMobiles.addAll(remindUsers);
                }
            }
            textObj.put("mentioned_mobile_list", atMobiles);
            bodyObj.put("markdown", textObj);
            bodyObj.put("msgtype", "markdown");
            JSONObject resultObj = wechatFeignClient.send(webhook, bodyObj);
            return resultObj != null && resultObj.get("errcode").equals(0);
        } catch (Exception e) {
            log.error("消息通知异常", e);
            return false;
        }

    }
}
